package com.ytjj.qmyx.admin.service.impl;

import com.ytjj.qmyx.admin.dao.ProductInfoStatisticsDao;
import com.ytjj.qmyx.admin.eunm.ActivityTypeNameCode;
import com.ytjj.qmyx.admin.model.request.ProductInfoRequest;
import com.ytjj.qmyx.admin.model.response.ProductInfoResponse;
import com.ytjj.qmyx.admin.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * 
 * @Version: 1.0
 */
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoStatisticsDao productInfoStatisticsDao;

    @Override
    public List<ProductInfoResponse> productInfoStatistics(ProductInfoRequest productInfoRequest) {

        // 时间处理 年-月-日
        String sTime = productInfoRequest.getDayTime() + " 00:00:00";
        String eTime = productInfoRequest.getDayTime() + " 23:59:59";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startTime = sdf.parse(sTime);
            Date endTime = sdf.parse(eTime);
            productInfoRequest.setStartTime(startTime);
            productInfoRequest.setEndTime(endTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<ProductInfoResponse> infoList = new ArrayList<>();

        if(productInfoRequest.getActivityType().equals(ActivityTypeNameCode.ORDINARY_PRODUCT.getCode())) {
            infoList = productInfoStatisticsDao.selList(productInfoRequest);
        } else {
            infoList = productInfoStatisticsDao.selActivityList(productInfoRequest);
            if (!CollectionUtils.isEmpty(infoList)) {
                for (ProductInfoResponse pi : infoList) {
                    // 活动类型--中文名称
                    if(pi.getActivityType() != null){
                        pi.setActivityTypeText(ActivityTypeNameCode.getName(pi.getActivityType()));
                    }
                }
            }
        }
        return infoList;
    }

}
